<?php
namespace Wechat\Controller;

class GoodstypeController extends BaseController{
    /*商品分类首页*/
    public function index()
    {
        $count      = M('goodstype')->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = M('goodstype')->order('goods_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(['page'=>$show,'data'=>$list]);
        $this->display();
    }

    /*商品分类添加*/

    public function add()
    {
        $post = i('post.');
        if(IS_POST){
            if(trim(empty($post['tname'])) || trim(empty($post['des']))){
                $this->error('非法提交');
            }
            if(M('goodstype')->where(['goods_name'=>$post['tname']])->find()){
                $this->error('已有相同分类名，请勿重复提交');
            }
            $data = [
                'goods_name'=>$post['tname'],
                'goods_des'=>$post['des'],
                'goods_maker'=>$_SESSION['name'],
                'goods_time'=>time(),
            ];
            if(M('goodstype')->add($data)){
                $this->success('Congratulation!  Successfully added!!!');
            }else{
                $this->error('未知错误');
            }
        }else {
            $this->display();
        }
    }
    /*商品分类修改*/
    public function edit()
    {
        $id = i('get.id');
        $post = i('post.');
        $old = M('goodstype')->where(['id'=>$id])->find();
        $this->assign('old',$old);
        if(IS_POST){
            if(trim(empty($post['tname'])) || trim(empty($post['des']))){
                $this->error('未知错误');
            }
            $data = [
                'goods_name'=>$post['tname'],
                'goods_des'=>$post['des'],
                'goods_maker'=>$_SESSION['name'],
                'goods_time'=>time(),
            ];
            if(M('goodstype')->where(['id'=>$id])->save($data)){
                $this->success('Congratulation!  Successfully edit!!!');
            }else{
                $this->error('未知错误');
            }
        }else {
            $this->display();
        }
    }
    /*商品分类删除*/
    public function del()
    {
        $id = i('get.id');
        if(M('goodstype')->where(['id'=>$id])->delete()){
            $this->success('Congratulation!  Successfully delete!!!');
        }else{
            $this->error('未知错误');
        }
    }


}